package com.dzjcloud.dzj.gateway.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.dzjcloud.dzj.common.redis.constant.RedisInfoConstant;
import com.dzjcloud.dzj.common.redis.utils.RedisUtil;
import com.dzjcloud.dzj.gateway.dto.Gateway;
import com.dzjcloud.dzj.gateway.service.impl.DynamicRouteServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: 董泽建
 * @create: 2024-03-26 17:30
 * @Description: 加载路由启动初始化
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ToRouterRunner implements ApplicationRunner {

    private final RedisUtil redisUtil;

    private final DynamicRouteServiceImpl dynamicRouteService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取路由信息
        String gatewayString=(String)redisUtil.get(RedisInfoConstant.GATEWAY_ROUTER_INFO_KEY);
        log.info("------当前路由信息：{}",gatewayString);
        if (StrUtil.isNotBlank(gatewayString)){
            List<Gateway> gateways = JSONArray.parseArray(gatewayString, Gateway.class);
            dynamicRouteService.refresh(gateways);
        }
    }
}
